<template>
    <el-dialog
            :title="!dataForm.customerId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="客户ID，主键" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="客户ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="客户名称" prop="customerName">
                        <el-input v-model="dataForm.customerName" placeholder="客户名称"></el-input>
                </el-form-item>
                <el-form-item label="客户类型，个人或企业" prop="customerType">
                        <el-input v-model="dataForm.customerType" placeholder="客户类型，个人或企业"></el-input>
                </el-form-item>
                <el-form-item label="联系方式，如电话、邮箱等" prop="contactInfo">
                        <el-input v-model="dataForm.contactInfo" placeholder="联系方式，如电话、邮箱等"></el-input>
                </el-form-item>
                <el-form-item label="所属行业" prop="industry">
                        <el-input v-model="dataForm.industry" placeholder="所属行业"></el-input>
                </el-form-item>
                <el-form-item label="注册日期" prop="registrationDate">
                        <el-date-picker
                                v-model="dataForm.registrationDate"
                                type="datetime"
                                placeholder="注册日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="客户地址" prop="address">
                        <el-input v-model="dataForm.address" placeholder="客户地址"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            customerId: null,
            customerName: null,
            customerType: null,
            contactInfo: null,
            industry: null,
            registrationDate: null,
            address: null
          },
          dataRule: {
            customerId: [
                        { required: true, message: '客户ID，主键不能为空', trigger: 'blur' }
            ],
            customerName: [
                        { required: true, message: '客户名称不能为空', trigger: 'blur' }
            ],
            customerType: [
                        { required: true, message: '客户类型，个人或企业不能为空', trigger: 'blur' }
            ],
            contactInfo: [
                        { required: true, message: '联系方式，如电话、邮箱等不能为空', trigger: 'blur' }
            ],
            industry: [
                        { required: true, message: '所属行业不能为空', trigger: 'blur' }
            ],
            registrationDate: [
                        { required: true, message: '注册日期不能为空', trigger: 'blur' }
            ],
            address: [
                        { required: true, message: '客户地址不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.customerId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.customerId = id || 0
            if (this.dataForm.customerId) {
              this.$http({
                url: this.$http.adornUrl(`/app/customer_info/info/${this.dataForm.customerId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appcustomerinfo
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/customer_info/${!this.dataForm.customerId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>